package com.aishua.fire.service.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aishua.fire.common.constant.DictConstants;
import com.aishua.fire.common.constant.SoftdelConstants;
import com.aishua.fire.entity.dict.AddDictInfo;
import com.aishua.fire.entity.dict.DictList;
import com.aishua.fire.entity.dict.UpdateDictInfo;
import com.aishua.fire.jpa.DictionaryDao;
import com.aishua.fire.jpa.entity.Dictionary;
import com.aishua.fire.mapper.DictMapper;
import com.aishua.fire.mapper.dto.DictDto;
import com.aishua.fire.service.DictService;

/**
 * 字典管理service层类
 * @author Administrator
 *
 */
@Service
public class DictServiceImpl implements DictService {
	
	@Autowired
	private DictMapper dictMapper;
	@Autowired
	private DictionaryDao dictionaryDao;
	@Override
	public String save(AddDictInfo dictInfo) {
		
		int countD = dictMapper.findCount(dictInfo.getDictValue());
		int countDt = dictMapper.findCou(dictInfo.getDictValue());
	if(countD == 0 && countDt >= 0){
		DictDto dict = new DictDto();
		dict.setCreateDate(new Date());
		dict.setStatus(SoftdelConstants.DEL_N);
		dict.setDictName(dictInfo.getDictName());
		dict.setTeamValue(dictInfo.getTeamValue());
		dict.setDictValue(dictInfo.getDictValue());
		dict.setTeamName(dictInfo.getTeamName());
		dictMapper.deleteBydicValue(dictInfo.getDictValue());
		dictMapper.save(dict);
		return DictConstants.DICT_S;
	  }else if(countD == 0 && countDt == 0) {
		  DictDto dict = new DictDto();
			dict.setCreateDate(new Date());
			dict.setStatus(SoftdelConstants.DEL_N);
			dict.setDictName(dictInfo.getDictName());
			dict.setTeamValue(dictInfo.getTeamValue());
			dict.setDictValue(dictInfo.getDictValue());
			dict.setTeamName(dictInfo.getTeamName());
			dictMapper.save(dict);
			return DictConstants.DICT_S;
	  }else{
		 
		  return DictConstants.DICT_N;
	  }
		
	}

	@Override
	public void deleteDict(String dictValue) {
		//修改为删除状态
		dictMapper.softDeleteDict(dictValue,SoftdelConstants.DEL_Y);
	}

	@Override
	public void update(UpdateDictInfo dictInfo) {
		DictDto dict = new DictDto();
		dict.setDictName(dictInfo.getDictName());
		dict.setTeamValue(dictInfo.getTeamValue());
		dict.setDictValue(dictInfo.getDictValue());
		dict.setTeamName(dictInfo.getTeamName());

		dictMapper.saveAndFlush(dict);
	}

	/**
	 * 查详情
	 */
	@Override
	public DictDto findDictId(String dictValue) {
		return dictMapper.findOne(dictValue);
	}

	/**
	 * 查询组别列表
	 */
	@Override
	public List<DictDto> findAllUsingByType(String teamValue) {
		return dictMapper.findAllUsingByType(teamValue, SoftdelConstants.DEL_N);
	}

	/**
	 * 分页列表条件查询
	 */
	@Override
	public List<DictDto> searchList() {
		return dictMapper.searchDictList(SoftdelConstants.DEL_N);
	}


	/**
	 * 根据dicValue(suppierId)查询数据字典表teamName
	 * @return
	 */
	public String findteamNameBydicValue(String dictValue) {
		return dictMapper.findteamNameBydicValue(dictValue);
	}
	
	public List<Dictionary> findDictName(String teamValue){
		return dictionaryDao.getDictName(teamValue,SoftdelConstants.DEL_N);
	}

	//查询dict_value,dict_name
	public List<DictList> getDicts(String teamValue) {
		Map<String, Object> pram=new HashMap<>();
		pram.put("teamValue", teamValue);
		pram.put("status", SoftdelConstants.DEL_N);
		List<DictList> dicts = dictMapper.getDicts(pram);
		return dicts;
	}
	
}
